export const hasownproperty = Object.prototype.hasOwnProperty

// 按对象属性排序 升序:asc   降序:des
export function compare (propertyName, type:string = 'asc') {
  return function (object1, object2) {
    let value1 = object1[propertyName]
    let value2 = object2[propertyName]
    if (value2 < value1) {
      return type === 'des' ? -1 : 1
    } else if (value2 > value1) {
      return type === 'des' ? 1 : -1
    } else {
      return 0
    }
  }
}

export function removeHMS (time) {
  if (time === undefined || time === null || time.toString() === '') return
  return time.slice(0, time.indexOf(' '))
}

/**
 * 格式化单位
 * @param {number} amount 原始数值
  * @param {number} [miniUnit=10000] 最小间隔单位
  * @param {number} [start=0] 起跳位置，0表示个，1表示十，以此类推
  * @param {number} [toFixed=4] 结果保留几位有效数字
  * @returns
 */
export function optimizeUnit (amount: number, miniUnit: number = 10000, start: number = 0, toFixed = 4) {
  const UNITS = ["", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿", "万亿"];
  if (amount === undefined || amount === null || amount.toString() === "") return amount;
    return Math.abs(amount) >= miniUnit ?
      optimizeUnit(amount / miniUnit, miniUnit, start + Math.log10(miniUnit), toFixed) :
      !isNaN(parseFloat(amount.toString())) ?
        validVal(amount, toFixed) + UNITS[start] : amount + UNITS[start];
  
  function validVal (amount:number, toFixed:number) {
    let pattern = /[1-9]([\.0-9]{3})/g;
    let amountStr = amount.toString()
    let result = pattern.exec(amountStr)
    if (result !== null) {
      let amountVal = Math.abs(parseFloat(amountStr))
      return amountVal >= 1000 || amountVal < 1
        ? amountStr.slice(0, result.index + toFixed)
        : amountStr.slice(0, result.index + toFixed + 1)
    } else {
      return amountStr
    }
  }
}

/**
 * 数据处理为绘图数据
 * @param {number} amount 原始数值
 * @param {number} [miniUnit=10000] 最小间隔单位
 * @param {number} [toFixed=2] 结果保留几位有效数字
 * @returns
 */
export function handleVal (amount: number, miniUnit: number = 10000, toFixed = 2) {
  const UNITS = ["", "万"]
  if (amount === undefined || amount === null || amount.toString() === "") return {
    val: amount,
    unit: UNITS[0]
  };
  let newAmount = parseFloat(amount.toString())
  return !isNaN(newAmount)
    ? {
        val: (newAmount / miniUnit).toFixed(toFixed),
        unit: UNITS[1]
      }
    : {
        val: amount,
        unit: UNITS[0]
      }
}

/**
 * 元／股 替换为万元／万股
 */
export function replaceUnit (content: string, unit: string = '') {
  let newContent = content
  let pattern = /\([股元]\)/g;
  let result = pattern.exec(newContent)
  if (result) {
    let strToArr = result[0].split('')
    strToArr.splice(1, 0, unit)
    let targetStr = strToArr.join('')
    let newStr = newContent.replace(pattern, targetStr)
    return newStr
  } else {
    return newContent
  }
}

// 判断对象是否没有任何属性
export function isEmptyObject(obj) {
  for (var key in obj) {
    return false;
  }
  return true;
}

// pie chart color
export const chartColors = {
  red: '#FF5C5C',
  orange: '#F6BE23',
  blue: '#298CFF'
}

// 股东人数与股价联动
export const SHnumAndPriceColors = {
  blue: 'rgba(6,104,255, 0.6)',
  orange: '#F76B1C'
}

// basic chart color => 棕色
export const basicChartColors = ['#F6A623']

// polar chart color 
export const polarChartColors = [
  'rgba(255, 99, 132, 0.7)',
  'rgba(255, 159, 64, 0.7)',
  'rgba(255, 205, 86, 0.7)',
  'rgba(75, 192, 192, 0.7)',
  'rgba(54, 162, 235, 0.7)',
  'rgba(153, 102, 255, 0.7)',
  'rgba(201, 203, 207, 0.7)',
  'rgba(229, 115, 115, 0.7)',
  'rgba(0,77,64, 0.5)',
  'rgba(255,109,0, 0.5)',
  'rgba(236,64,122, 0.5)',
  'rgba(66,165,245, 0.5)',
  'rgba(104,159,56, 0.6)',
  'rgba(100,221,23, 0.5)',
  'rgba(0,188,212, 0.6)',
  'rgba(205,220,57, 0.6)',
  'rgba(3,169,244, 0.6)',
  'rgba(102,187,106, 0.5)',
  'rgba(230,81,0, 0.6)',
  'rgba(92,107,192, 0.5)',
  'rgba(230,74,25, 0.5)',
  'rgba(38,166,154, 0.6)',
  'rgba(249,168,37, 0.6)',
  'rgba(126,87,194, 0.5)',
  'rgba(97,97,97, 0.6)',
  'rgba(213,0,249, 0.6)',
  'rgba(101,31,255, 0.5)',
  'rgba(197,17,98, 0.5)',
  'rgba(96,125,139, 0.5)',
  'rgba(255,23,68, 0.5)',
  'rgba(171,71,188, 0.5)',
  'rgba(255,87,34, 0.5)'
]

// chart util
export const chartjsColors = {
	red: 'rgba(255, 155, 155, 0.8)',
	orange: 'rgb(255, 159, 64)',
	yellow: 'rgb(255, 205, 86)',
	green: 'rgb(75, 192, 192)',
	blue: 'rgba(89, 180, 255, 0.8)',
	purple: 'rgba(148, 145, 255, 0.8)',
	grey: 'rgb(201, 203, 207)'
};

export function randomScalingFactor () {
	return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
};